package top.hzy520.config;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.hzy520.job.IncreaseUserPointsSimpleJob;
import top.hzy520.job.PostPointsMessageSimpleJob;
import top.hzy520.job.StockSimpleJob;

/**
 * @Author: HouZeYu
 * @Description:  任务配置中心
 * @Date: Created in 17:17 2018/7/7
 */
@Configuration
public class TaskConfig {

    @Autowired
    private ZookeeperRegistryCenter regCenter;

    @Bean
    public SimpleJob simpleJob() {
        return new StockSimpleJob();
    }
    @Bean
    public PostPointsMessageSimpleJob postPointsMessageSimpleJob() {
        return new PostPointsMessageSimpleJob();
    }
    @Bean
    public IncreaseUserPointsSimpleJob increaseUserPointsSimpleJob() {
        return new IncreaseUserPointsSimpleJob();
    }
    @Bean(initMethod = "init")
    public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${stockJob.cron}") final String cron, @Value("${stockJob.shardingTotalCount}") final int shardingTotalCount,
                                           @Value("${stockJob.shardingItemParameters}") final String shardingItemParameters) {
        System.out.println("stock被初始化");
        return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters));
    }
    @Bean(initMethod = "init")
    public JobScheduler PostPointsMessageSimpleJobScheduler(final PostPointsMessageSimpleJob simpleJob, @Value("${PostPointsMessageJob.cron}") final String cron, @Value("${PostPointsMessageJob.shardingTotalCount}") final int shardingTotalCount,
                                           @Value("${PostPointsMessageJob.shardingItemParameters}") final String shardingItemParameters) {
        System.out.println("PostPointsMessageSimpleJob被初始化");
        return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters));
    }
    @Bean(initMethod = "init")
    public JobScheduler increaseUserPointsSimpleJobScheduler(final IncreaseUserPointsSimpleJob increaseUserPointsSimpleJob, @Value("${increaseUserPointsJob.cron}") final String cron, @Value("${increaseUserPointsJob.shardingTotalCount}") final int shardingTotalCount,
                                                             @Value("${increaseUserPointsJob.shardingItemParameters}") final String shardingItemParameters) {
        System.out.println("increaseUserPointsSimpleJob被初始化");
        return new SpringJobScheduler(increaseUserPointsSimpleJob, regCenter, getLiteJobConfiguration(increaseUserPointsSimpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters));
    }
    private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
        return LiteJobConfiguration
                .newBuilder(
                        new SimpleJobConfiguration(JobCoreConfiguration
                                .newBuilder(
                jobClass.getName(), cron, shardingTotalCount)
                                .shardingItemParameters(shardingItemParameters)
                                .build(), jobClass.getCanonicalName()))
                .overwrite(true)
                .build();
    }
}
